package com.inspur.sc.springboot.controller;

import com.inspur.sc.springboot.entity.SysUser;
import com.inspur.sc.springboot.utils.SpringSecurityUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Autowired
    private UserDetailsService customUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /*@ApiOperation(value = "用户登陆")
    @PostMapping(value = "/login")
    public String login(HttpServletRequest request,
                        @ApiParam(value = "用户名") String username,
                        @ApiParam(value = "密码") String password){
        SysUser user= (SysUser) customUserService.loadUserByUsername(username);
        if(null != user){
            if(passwordEncoder.matches(password,user.getPassword())){
                SpringSecurityUtils.saveUserDetailsToContext(user,request);
                return "redirect:/admin/user";
            }
            return "redirect:/loginPage?error=true";
        }
        return "redirect:/loginPage?error=true";
    }*/


   /* @RequestMapping(value="/logout", method = RequestMethod.GET)
    public String logout (HttpServletRequest request, HttpServletResponse response) {
        SecurityContext securityContext=SecurityContextHolder.getContext();
        Authentication auth = securityContext.getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return "redirect:/index?logout";
    }*/

}
